package cn.rokhdelar.ams.management.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.rokhdelar.ams.common.domain.vo.PageVO;
import cn.rokhdelar.ams.management.domain.po.SysAuthority;
import cn.rokhdelar.ams.management.domain.query.AuthorityQuery;
import cn.rokhdelar.ams.management.domain.vo.AuthorityVO;
import cn.rokhdelar.ams.management.service.SysAuthorityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/management/authorities")
@Schema(title = "权限接口", description = "权限接口")
@RequiredArgsConstructor
public class AuthorityController {
    private final SysAuthorityService authorityService;

    @GetMapping
    @Operation(summary = "根据条件分页查询权限")
    public ResponseEntity<PageVO<AuthorityVO>> listAuthorities(@ParameterObject AuthorityQuery query) {
        return ResponseEntity.ok(authorityService.getAuthorityListByPage(query));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询权限")
    public ResponseEntity<AuthorityVO> getAuthority(@PathVariable String id) {
        return ResponseEntity.ok(authorityService.getAuthorityById(id));
    }

    @PostMapping
    @Operation(summary = "创建新的权限信息")
    public ResponseEntity<Boolean> createAuthority(@RequestBody AuthorityVO authorityVO) {
        SysAuthority authority = BeanUtil.copyProperties(authorityVO, SysAuthority.class);
        return ResponseEntity.ok(authorityService.save(authority));
    }

    @PatchMapping
    @Operation(summary = "根据ID更新权限信息")
    public ResponseEntity<Boolean> updateAuthority(@RequestBody AuthorityVO authorityVO) {
        SysAuthority authority = BeanUtil.copyProperties(authorityVO, SysAuthority.class);
        return ResponseEntity.ok(authorityService.saveOrUpdate(authority));
    }

    @DeleteMapping(value = "/{id}")
    @Operation(summary = "根据ID删除权限")
    public ResponseEntity<Boolean> deleteAuthority(@PathVariable String id) {
        return ResponseEntity.ok(authorityService.removeById(id));
    }

}
